package cn.com.shopec.erp.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.commo.AuditSettingPropertyUtils;
import cn.com.shopec.erp.base.dao.IAuditSettingDao;
import cn.com.shopec.erp.base.entity.AuditSetting;
import cn.com.shopec.erp.base.service.IAuditSettingService;
import cn.com.shopec.erp.base.vo.AuditSettingVo;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.StringUtil;

/**
 * @ClassName: AuditSettingServiceImpl
 * @Description: 审核金额设置service接口实现
 * @author: gaoyaxiong
 * @date: 2013-5-26 下午11:14:13
 */
@Service
public class AuditSettingServiceImpl implements IAuditSettingService {

	@Resource
	private AuditSettingPropertyUtils asPropertyUtils;

	@Resource
	private IAuditSettingDao auditSettingDao;

	/**
	 * 根据用户角色查询审核金额设置
	 * 
	 * @param userRole
	 * @return AuditSettingVo
	 * @throws Exception
	 */
	@Override
	public AuditSettingVo getAuditSetting(String userRole) {
		AuditSetting as = auditSettingDao.get("userRole", userRole);
		return asPropertyUtils.getAuditSettingVo(as);
	}

	
	/**
	 * 分页查询审核金额设置
	 * 
	 * @param AuditSettingVo
	 * @return PageFinder<AuditSettingVo>
	 * @throws Exception
	 */
	@Override
	public PageFinder<AuditSettingVo> getPageFinderAuditSetting(AuditSettingVo AuditSettingVo,PageQuery pageQuery) {
		DetachedCriteria dc = auditSettingDao.createCriteria();
		PageFinder<AuditSetting> pageFinder = auditSettingDao.pagedByCriteria(dc,pageQuery.getPage(),pageQuery.getPageSize());

		List<AuditSettingVo> asVoList = new ArrayList<AuditSettingVo>();
		for (AuditSetting as : pageFinder.getData()) {
			asVoList.add(asPropertyUtils.getAuditSettingVo(as));
		}
		PageFinder<AuditSettingVo> asVoPageFinder = new PageFinder<AuditSettingVo>(pageFinder.getPageNo(), pageFinder.getPageSize(),pageFinder.getRowCount());
		asVoPageFinder.setData(asVoList);
		return asVoPageFinder;
	}

	
	/**
	 * 新增审核金额设置
	 * 
	 * @param AuditSettingPropertyUtils
	 * @return
	 * @throws Exception
	 */
	@Override
	public String addAuditSetting(AuditSettingVo asVo) {

		try {
			// 判断角色是否已添加
			if (auditSettingDao.get("userRole", asVo.getUserRole()) != null) {
				return "你选择的角色已经添加";
			}
			AuditSetting as = new AuditSetting();
			as = asPropertyUtils.getAuditSetting(asVo);
			as.setAuditState(1);//已审核
			as.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
			as.setCode(SZSCMConstant.genUUID());//自动生成code
			auditSettingDao.save(as);
		} catch (Exception e) {
			e.printStackTrace();
			return SZSCMConstant.FAIL;
		}
		return SZSCMConstant.SUCCESS;
	}

	
	/**
	 * 修改审核金额设置
	 * 
	 * @param AuditSettingPropertyUtils
	 * @return
	 * @throws Exception
	 */
	@Override
	public String updateAuditSetting(AuditSettingVo asVo) {
		AuditSetting as = new AuditSetting();
		as = asPropertyUtils.getAuditSetting(asVo);
		try {
			auditSettingDao.update(as);
		} catch (Exception e) {
			e.printStackTrace();
			return SZSCMConstant.FAIL;
		}
		return SZSCMConstant.SUCCESS;
	}

	
	@Override
	public AuditSetting getAuditSettingById(String code) {
		return auditSettingDao.findUniqueBy("code", code);
	}


	@Override
	@Transactional
	public AuditSetting updateAuditSetting(AuditSetting auditSetting) throws Exception {
		AuditSetting set = getAuditSettingById(auditSetting.getCode());
		set.setMoney(auditSetting.getMoney());
		//set.setAuditState(auditSetting.getAuditState());
		return auditSettingDao.update(set);
	}


	@Override
	public boolean checkNeedsAudit(int role, String amount) {
		DetachedCriteria criteria = this.auditSettingDao.createCriteria();
		if(StringUtil.isNumber(String.valueOf(role))){
			criteria.add(Restrictions.eq("userRole", role));
		}
		criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<AuditSetting> AuditSettingList  = this.auditSettingDao.findByCriteria(criteria);
		double str = Double.valueOf(amount);
		if(AuditSettingList != null && AuditSettingList.size() > 0){
			if(AuditSettingList.get(0).getAuditState() == 1 && Double.valueOf(AuditSettingList.get(0).getMoney()) <= str){
				return true;
			}
		}
		return false;
	}

}
